package de.osmembrane.source_file_header;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Main file for SourceFileHeader, adds a given header automatically to all
 * matching files.
 * 
 * Important: header must be inside of the / * ... * / comment-marks.
 * 
 * @author jakob_jarosch
 */
public class Main {

	public static void main(String[] args) throws IOException {
		if (args.length != 3) {
			System.out
					.println("Usage: java -jar SourceFileHeader.jar <folder> <headerFile> <filterRegexPattern>");
			System.out.println("Example:");
			System.out
					.println("	java -jar SourceFileHeader.jar \"src/\" \"header.txt\" \"\\.java$\"");
			System.exit(1);
		}

		String header = readFile(new File(args[1]));

		File[] files = new Filer(new File(args[0])).searchRecursivly(args[2]);

		System.out.println("Found " + files.length + " files.");

		for (File file : files) {
			System.out.println("Replacing file " + file.getAbsolutePath());
			Replacer repl = new Replacer(file);
			repl.replace(header);
		}
	}

	private static String readFile(File file) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(file));
		String line;
		StringBuilder builder = new StringBuilder();
		while ((line = in.readLine()) != null) {
			builder.append(line + "\n");
		}
		in.close();

		return builder.toString();
	}
}
